package com.next.aap.server.persistance.helper;

import java.util.List;

import org.springframework.stereotype.Component;

import com.next.aap.server.persistance.AssemblyConstituency;
import com.next.core.db.page.PageInfo;
import com.next.core.db.page.PageResult;
import com.next.core.db.page.QueryParam;
import com.next.core.db.validator.ObjectValidator;
import com.next.core.exception.AppException;
import com.next.ext.core.db.page.HibernateQueryParamPageInfo;
import com.next.ext.core.db.page.HibernateQueryParamPageInfo.ORDER;
import com.next.ext.core.persistance.dao.BasePersistanceDao;

@Component
public class AssemblyConstituencyHelper extends BasePersistanceDao<AssemblyConstituency> implements ObjectValidator<AssemblyConstituency> {

	/**
	 * Creates/updates a assemblyConstituency in Database
	 * 
	 * @param assemblyConstituency
	 * @return saved assemblyConstituency
	 * @throws AppException
	 */
	public AssemblyConstituency saveAssemblyConstituency(AssemblyConstituency assemblyConstituency) throws AppException
	{
		checkIfAssemblyConstituencyExistsWithSameName(assemblyConstituency);
		assemblyConstituency.setNameUp(assemblyConstituency.getName().toUpperCase());
		assemblyConstituency = super.saveObject(assemblyConstituency);
		return assemblyConstituency;
	}

	/**
	 * deletes a assemblyConstituency in Database
	 * 
	 * @param assemblyConstituency
	 * @return updated assemblyConstituency
	 * @throws AppException
	 */
	public void deleteAssemblyConstituency(AssemblyConstituency assemblyConstituency) throws AppException {
		super.deleteObject(assemblyConstituency);
	}

	/**
	 * return a AssemblyConstituency with given primary key/id
	 * 
	 * @param id
	 * @return AssemblyConstituency with PK as id(parameter)
	 * @throws AppException
	 */
	public AssemblyConstituency getAssemblyConstituencyById(Long id) throws AppException
	{
		AssemblyConstituency assemblyConstituency = super.getObjectByPK(AssemblyConstituency.class, id);
		return assemblyConstituency;
	}

	/**
	 * @param pageInfo
	 * @return search result
	 * @throws AppException
	 */
	public PageResult<AssemblyConstituency> searchAssemblyConstituencys(PageInfo pageInfo) throws AppException
	{
		return super.findObject(AssemblyConstituency.class, pageInfo);
	}

	public List<AssemblyConstituency> getAllAssemblyConstituencys() throws AppException
	{
		PageInfo pageInfo = null;
		return super.findObject(AssemblyConstituency.class, pageInfo).getResultList();
	}
	public List<AssemblyConstituency> getAssemblyConstituencyOfDistrict(long districtId) throws AppException{
		HibernateQueryParamPageInfo pageInfo = new HibernateQueryParamPageInfo();
		QueryParam districtQueryParam = new QueryParam();
		districtQueryParam.setCaseSenstive(false);
		districtQueryParam.setField("districtId");
		districtQueryParam.setFieldType(QueryParam.FIELD_TYPE_NUMBER);
		districtQueryParam.setOperator(QueryParam.OPERATOR_EQUAL);
		districtQueryParam.setValue(districtId);
		pageInfo.addCriteria(districtQueryParam);
		pageInfo.addOrderBy("name", ORDER.ASC);
		
		PageResult<AssemblyConstituency> pr = this.searchAssemblyConstituencys(pageInfo);
		List<AssemblyConstituency> assemblyConstituencyList = pr.getResultList();
		return assemblyConstituencyList;
	}
	public void validateObjectForCreate(AssemblyConstituency assemblyConstituency) throws AppException {
		checkIfStringMissing("Name", assemblyConstituency.getName());
		checkIfObjectMissing("District", assemblyConstituency.getDistrict());
	}

	public void validateObjectForUpdate(AssemblyConstituency assemblyConstituency) throws AppException {
		checkIfStringMissing("Name", assemblyConstituency.getName());
		checkIfObjectMissing("District", assemblyConstituency.getDistrict());
	}
	private void checkIfAssemblyConstituencyExistsWithSameName(AssemblyConstituency assemblyConstituency) throws AppException{
		HibernateQueryParamPageInfo pageInfo = new HibernateQueryParamPageInfo();
		QueryParam emailQueryParam = new QueryParam();
		emailQueryParam.setCaseSenstive(false);
		emailQueryParam.setField("nameUp");
		emailQueryParam.setFieldType(QueryParam.FIELD_TYPE_STRING);
		emailQueryParam.setOperator(QueryParam.OPERATOR_EQUAL);
		emailQueryParam.setValue(assemblyConstituency.getName().toUpperCase());
		pageInfo.addCriteria(emailQueryParam);
		
		QueryParam districtQueryParam = new QueryParam();
		districtQueryParam.setField("districtId");
		districtQueryParam.setFieldType(QueryParam.FIELD_TYPE_NUMBER);
		districtQueryParam.setOperator(QueryParam.OPERATOR_EQUAL);
		districtQueryParam.setValue(assemblyConstituency.getDistrict().getId());
		pageInfo.addCriteria(districtQueryParam);
		
		if(assemblyConstituency.getId() != null && assemblyConstituency.getId() > 0){
			QueryParam assemblyConstituencyIdQueryParam = new QueryParam();
			assemblyConstituencyIdQueryParam.setCaseSenstive(false);
			assemblyConstituencyIdQueryParam.setField("id");
			assemblyConstituencyIdQueryParam.setFieldType(QueryParam.FIELD_TYPE_NUMBER);
			assemblyConstituencyIdQueryParam.setOperator(QueryParam.OPERATOR_NOT_EQUAL);
			assemblyConstituencyIdQueryParam.setValue(assemblyConstituency.getId());
			pageInfo.addCriteria(assemblyConstituencyIdQueryParam);
		}
		
		PageResult<AssemblyConstituency> pr = this.searchAssemblyConstituencys(pageInfo);
		if(pr.getResultList().size() > 0){
			throw new AppException("AssemblyConstituency already exists with name = "+assemblyConstituency.getName()+" in district "+assemblyConstituency.getDistrict().getName());
		}
	}
}